<?php
/**
 * Template Name: Project Search
 * Description: A page used to search for projects
 *
 * Author: Andrey Brushchenko
 * Date: 8/22/2013
 */
get_header(); ?>

<?php 
$defaultposts = 5;

if($_GET['x'] > -1)
	$offset = $_GET['x'];
else
	$offset = 0;

$baseurl = $_SERVER['SCRIPT_NAME'] . "?page_id=" . $_GET['page_id'];
if($_GET['category'])
	$baseurl .= "&category=" . $_GET['category'];
if($_GET['order'])
	$baseurl .= "&order=" . $_GET['order'];
if($_GET['results'])
	$baseurl .= "&results=" . $_GET['results'];

if($_GET['results']) {
	$args = array('posts_per_page' => $_GET['results'], 
			'offset' => $offset * $_GET['results'],
			'order' => $_GET['order'], 
			'orderby' => $_GET['category'],
			'post_status' => 'publish',
			'suppress_filters' => true);
}
else {
	$args = array('posts_per_page' => 5, 
			'offset' => $offset * $defaultposts,
			'order' => 'DES', 
			'orderby' => 'date',
			'post_status' => 'publish',
			'suppress_filters' => true);
}
$postslist = get_posts($args); 
?>

<html>
<div id="projectsearch">
	<div id="pagetitle">Projects</div>

	<?php foreach($postslist as $post): setup_postdata($post)?>
	<div id="projectsearchbox">
		<div id="projectsearchheader">
			<div id="projectsearchtitle">
				<a class="projectsearch" href="http://students.washington.edu/andreybr/wordpress/?p=<?php echo get_the_ID() ?>">
				<?php the_title() ?>
				</a>
			</div>
			<div id="projectsearchauthor">
				<a class="projectsearch" href="http://students.washington.edu/andreybr/wordpress/?page_id=208&user=<?php the_author_meta('ID'); ?>">
					<?php the_author() ?> 
				</a> 
			</div>
		</div>
		<a id="projectsearchimage" href="<?php the_permalink(); ?>">
			<?php if(has_post_thumbnail()) {?>
				<?php the_post_thumbnail(array(140,140)); ?>
			<?php } else { ?> 
				<img src="<?php bloginfo('template_directory'); ?>/images/blank.png" width="140" height="140" />
			<?php } ?>
		</a>
		<div id="projectsearchmeta">
			<div id="rating" class="projectsearchmetatag">9.5</div>
			<div class="projectsearchmetatag">
				<?php 
					$comments_count = wp_count_comments(get_the_ID());
					echo $comments_count->approved . " Comments";
				?>
			</div>
			<div class="projectsearchmetatag">University of Washington</div>	
			<div class="projectsearchmetatag">
				<?php the_time('m/d/y') ?>
			</div>
			<div id="projectsearchbar"></div>
			<div id="projectsearchdescription">	
				<?php the_content() ?>
			</div>
		</div>
	</div>
	<?php endforeach; ?>
</div>

<div id="projectsearchfilter">
	<div id="pagetitle">Search</div>
	<form method="get">
		<input type="hidden" name="page_id" value=<?php echo $_GET['page_id'] ?>>
		<div class="filteroption">
			Filter
			<select class="filterselect" name="category">
  				<option value="date">Newest</option>
 				<option value="comment_count">Most Comments</option>
  				<option value="rating">Highest Rating</option>
			</select>
		</div>
		<div class="filteroption">
			Order
			<select class="filterselect" name="order">
 				<option value="DES">Descending</option>
  				<option value="ASC">Ascending</option>
			</select>
		</div>
		<div class="filteroption">
			Results Per Page
			<select class="filterselect" name="results">
  				<option value=5>5</option>
 				<option value=10>10</option>
				<option value=20>20</option>
			</select>
		</div>
		<input type="submit" value="Search">
	</form>
</div>

<div id="pagebar">
	Page
	<?php
	if($_GET['x'] > -1) 		
		$currentpage = $_GET['x'];
	else 
		$currentpage = 0;

	$count_posts = wp_count_posts()->publish;

	if($_GET['results'] > 0)
		$numpages =  $count_posts / $_GET['results'];
	else
		$numpages =  $count_posts / $defaultposts;

	$numpages = ceil($numpages);

	$count = 0;
	while($numpages > $count) :
		if($count == $_GET['x'])
			echo "<b>";
		echo "<a href=\"" . $baseurl . "&x=" . $count . "\" id=\"pagelink\">" . ($count + 1) . "</a>";
		if($count == $_GET['x'])
			echo "</b>";
		$count++;
	endwhile;
	?>
</div>
<html>

<?php get_footer(); ?>
